//
//  ComposeToolBar.swift
//  weibobbaj
//
//  Created by Tiffany宝 on 16/4/26.
//  Copyright © 2016年 Tiffany. All rights reserved.
//

import UIKit

// 使用枚举去区分不同的按钮点击
enum ComposeToolBarButtonType: Int{
    
    case Picture = 0,Mention,Trend,Keyboard,Add
    
}

class ComposeToolBar: UIStackView {
    
    var isSystemKeyboard: Bool = true{
        
        didSet{
            
            //可以通过按钮对应的 tag 值,取到所选择的按钮
            let button = self.viewWithTag((ComposeToolBarButtonType.Keyboard).rawValue) as! UIButton
            
//            var name = "compose_emoticonbutton_background"
//            if isSystemKeyboard {
//                
////                button.setImage(UIImage(named: "compose_emoticonbutton_background"), forState: UIControlState.Normal)
////                button.setImage(UIImage(named: "compose_emoticonbutton_background__highlighted"), forState: UIControlState.Highlighted)
//                name = "compose_keyboardbutton_background"
//            }else{
//                
////                button.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: UIControlState.Normal)
////                button.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: UIControlState.Highlighted)
//                name = "compose_emoticonbutton_background"
//            }
            
            let name = isSystemKeyboard ? "compose_keyboardbutton_background" : "compose_emoticonbutton_background"
            
            button.setImage(UIImage(named: name), forState: UIControlState.Normal)
            button.setImage(UIImage(named: name + "_highlighted"), forState: UIControlState.Highlighted)
        }
    }
    
    //闭包
    var buttonClickClosure: ((type: ComposeToolBarButtonType) -> ())?
    
    // UIStackView 只是一个容器
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        distribution = .FillEqually
//        addSubview(composeButton)
        setComposeButton("compose_toolbar_picture",type:.Picture)
        setComposeButton("compose_mentionbutton_background",type:.Mention)
        setComposeButton("compose_trendbutton_background",type:.Trend)
        setComposeButton("compose_keyboardbutton_background",type:.Keyboard)
        setComposeButton("compose_add_background",type:.Add)
    }

    private func setComposeButton(imgName: String,type: ComposeToolBarButtonType){
        
        let composeButton: UIButton = UIButton()
        //给按钮打标签
        // 取到枚举的原始值
        composeButton.tag = type.rawValue
        //添加点击事件
        composeButton.addTarget(self, action: "childButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        composeButton.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
        composeButton.setImage(UIImage(named: imgName + "_highlighted"), forState: UIControlState.Highlighted)
        composeButton.setBackgroundImage(UIImage(named:"compose_toolbar_background"), forState: UIControlState.Normal)
        composeButton.setBackgroundImage(UIImage(named:"compose_toolbar_background"), forState: UIControlState.Highlighted)
//        composeButton.sizeToFit()
        addArrangedSubview(composeButton)
    }
    
    @objc private func childButtonClick(button: UIButton){
        
        //执行闭包
        buttonClickClosure!(type: ComposeToolBarButtonType(rawValue: button.tag)!)
    }
}
